//
//  SEGPublishHouseViewModel.m
//  Commom
//
//  Created by 梁学彰 on 2020/6/4.
//

#import "SEGPublishHouseViewModel.h"
NSString * const SEGPublishHouseViewCellIndetifier = @"SEGPublishHouseViewCellIndetifier";
@implementation SEGPublishHouseViewModel
-(instancetype)init{
    if (self == [super init]) {
        _title = @"";
        _placeholder = @"";
        _content = @"";
        _paraKey = @"";
        _widgetWidth = [UIScreen mainScreen].bounds.size.width;
        _widgetHeight = kPublishHouseViewHeight;
    }
    return self;
}
@end

/**************************SEGPublishHouseViewLayout************************/
@implementation SEGPublishHouseViewLayout
- (instancetype)initWithData:(SEGPublishHouseViewModel *)data type:(SEGEnumPublishHouseViewType)type{
    if (self == [super init]) {
        _data = data;
        _type = type;
    }
    [self segPublishHouseViewLayout];
    return self;
}

-(void)segPublishHouseViewLayout{
    //高度固定
    _totalHeight = _data.widgetHeight;
    
    //高度不固定
    //_totalHeight = 0;
    _titleLabelHeight = 0;
    _titleLabelTextLayout = nil;
    _placeLabelHeight = 0;
    _placeLabelTextLayout = nil;
    _contentLabelHeight = 0;
    _contentLabelTextLayout = nil;
    
    if (!_data) return;
    
    if (self.type == SEGEnumPublishHouseViewType_NO ||
        self.type == SEGEnumPublishHouseViewType_NO_Edit ||
        self.type == SEGEnumPublishHouseViewType_TextField_DecimalPad ||
        self.type == SEGEnumPublishHouseViewType_TextField_NumberPad ||
        self.type == SEGEnumPublishHouseViewType_Title ||
        self.type == SEGEnumPublishHouseViewType_Content ||
        self.type == SEGEnumPublishHouseViewType_TextField_NumbersAndPunctuation||
        self.type == SEGEnumPublishHouseViewType_TextField_Text) {
        _totalHeight = kPublishHouseViewHeight;
        [self layout];
    }
    if (self.type == SEGEnumPublishHouseViewType_Content) {
        _totalHeight = 135;
    }
}

//数据处理
-(void)layout{
    [self segtitleLabelWithContent:self.data.title fontSize:15 textColor:kColor_1];
    [self segplaceLabelWithContent:self.data.placeholder fontSize:15 textColor:kColor_C];
    [self segcontentLabelWithContent:self.data.content fontSize:15 textColor:kColor_1];
}
//控件titleLabel文本布局计算
-(void)segtitleLabelWithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    UIFont *font = [UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 1;
    modifier.paddingBottom = 1;
    //modifier.lineHeightMultiple = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(_data.widgetWidth, HUGE);
    container.linePositionModifier = modifier;
    container.truncationType = SEGTextTruncationTypeEnd;// "abcd…"
    
    _titleLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if (!_titleLabelTextLayout)return;
    _titleLabelHeight = [modifier heightForLineCount:_titleLabelTextLayout.rowCount];
}

//控件placeLabel文本布局计算
-(void)segplaceLabelWithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    UIFont *font = [UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 1;
    modifier.paddingBottom = 1;
    //modifier.lineHeightMultiple = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(_data.widgetWidth, HUGE);
    container.linePositionModifier = modifier;
    container.truncationType = SEGTextTruncationTypeEnd;// "abcd…"
    
    _placeLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if (!_placeLabelTextLayout)return;
    _placeLabelHeight = [modifier heightForLineCount:_placeLabelTextLayout.rowCount];
}

//控件contentLabel文本布局计算
-(void)segcontentLabelWithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    UIFont *font = [UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 1;
    modifier.paddingBottom = 1;
    //modifier.lineHeightMultiple = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake( LYScreenWidth() *0.8, HUGE);
    container.linePositionModifier = modifier;
    container.truncationType = SEGTextTruncationTypeEnd;// "abcd…"
    container.maximumNumberOfRows = 1;
    
    _contentLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if (!_contentLabelTextLayout)return;
    _contentLabelHeight = [modifier heightForLineCount:_contentLabelTextLayout.rowCount];
}

@end
